package cryptut

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"os"
)

func GenerateRSAKey(bits int) {
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		panic(err)
	}
	X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
	privateFile, err := os.Create("private.pem")
	if err != nil {
		panic(err)
	}
	defer privateFile.Close()
	privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
	pem.Encode(privateFile, &privateBlock)
	publicKey := privateKey.PublicKey
	X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
	if err != nil {
		panic(err)
	}
	publicFile, err := os.Create("public.pem")
	if err != nil {
		panic(err)
	}
	defer publicFile.Close()
	publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
	pem.Encode(publicFile, &publicBlock)
}

func RsaEncrypt(plain interface{}, publicKey interface{}) (string, error) {
	var plainBytes []byte
	var publicKeyBytes []byte
	switch plain.(type) { //这里是通过i.(type)来判断是什么类型  下面的case分支匹配到了 则执行相关的分支
	case string:
		plainBytes = str2bytes(plain.(string))
		break
	case []byte:
		plainBytes = plain.([]byte)
		break
	}
	if plainBytes == nil {
		return "", errors.New("type of plain is error !")
	}
	switch publicKey.(type) { //这里是通过i.(type)来判断是什么类型  下面的case分支匹配到了 则执行相关的分支
	case string:
		publicKeyBytes = str2bytes(publicKey.(string))
		break
	case []byte:
		publicKeyBytes = publicKey.([]byte)
		break
	}
	if publicKeyBytes == nil {
		return "", errors.New("type of publicKey is error !")
	}
	block, _ := pem.Decode(publicKeyBytes)
	publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		panic(err)
	}
	public := publicKeyInterface.(*rsa.PublicKey)
	cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, public, plainBytes)
	if err != nil {
		panic(err)
	}
	return base64.StdEncoding.EncodeToString(cipherText), nil
}

func RsaDecrypt(plain interface{}, privateKey interface{}) ([]byte, error) {
	var plainBytes []byte
	var privateKeyBytes []byte
	switch plain.(type) { //这里是通过i.(type)来判断是什么类型  下面的case分支匹配到了 则执行相关的分支
	case string:
		decoded, err := base64.StdEncoding.DecodeString(plain.(string))
		if err != nil {
			panic(err)
		}
		plainBytes = decoded
		break
	case []byte:
		plainBytes = plain.([]byte)
		break
	}
	if plainBytes == nil {
		return nil, errors.New("type of plain is error !")
	}
	switch privateKey.(type) { //这里是通过i.(type)来判断是什么类型  下面的case分支匹配到了 则执行相关的分支
	case string:
		privateKeyBytes = str2bytes(privateKey.(string))
		break
	case []byte:
		privateKeyBytes = privateKey.([]byte)
		break
	}
	if privateKeyBytes == nil {
		return nil, errors.New("type of publicKey is error !")
	}
	block, _ := pem.Decode(privateKeyBytes)
	privateKeyInterface, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		panic(err)
	}
	cipherText, err := rsa.DecryptPKCS1v15(rand.Reader, privateKeyInterface, plainBytes)
	if err != nil {
		panic(err)
	}
	return cipherText, nil
}

func RsaEncryptByKeyFile(plain interface{}, publicKey string) (string, error) {
	file, err := os.Open(publicKey)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	info, _ := file.Stat()
	buf := make([]byte, info.Size())
	file.Read(buf)
	return RsaEncrypt(plain, buf)
}

func RsaDecryptByKeyFile(plain interface{}, privateKey string) ([]byte, error) {
	file, err := os.Open(privateKey)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	info, _ := file.Stat()
	buf := make([]byte, info.Size())
	file.Read(buf)
	return RsaDecrypt(plain, buf)
}
